package org.iori.luodan.task;

import java.util.ArrayList;

import org.iori.luodan.base.BaseLogicFactory;
import org.iori.luodan.base.BaseStatListener;
import org.iori.luodan.entity.ApnsInfoEntity;
import org.iori.luodan.entity.DeviceInfoEntity;
import org.iori.luodan.entity.UserApnsInfoEntity;
import org.iori.luodan.logic.DeviceLogic;

import com.iori.common.constant.ArchConstant;
import com.iori.common.db.extend.DBInitClass;
import com.iori.common.util.PropertiesUtil;
import com.notnoop.apns.APNS;
import com.notnoop.apns.ApnsService;

public class SendMessageToApns extends BaseStatListener {
	
	private static DeviceLogic deviceLogic = BaseLogicFactory.getDeviceLogic();
	
	private ApnsService service = null;
	
	public SendMessageToApns() {
		PropertiesUtil.init();
		DBInitClass.init();
		
		String rootClassPath = SendMessageToApns.class.getClassLoader().getResource("/").getPath();
		try {
			ApnsInfoEntity apnsInfoEntity = deviceLogic.getApnsInfo();
			String certificate_file = rootClassPath + apnsInfoEntity.getCertificate_file().trim();
			String pass_word = apnsInfoEntity.getPass_word().trim();
			service = APNS.newService()
					.withCert(certificate_file, pass_word)
					.withSandboxDestination()
					.build();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Override
	protected void doExcute() {
		// TODO Auto-generated method stub
		try {
			ApnsInfoEntity apnsInfoEntity = deviceLogic.getApnsInfo();
			Boolean is_send = apnsInfoEntity.getIs_send();
			if (false == is_send) {
				return;
			}
			
			String message = apnsInfoEntity.getMessage().trim();
			if ("".equals(message)) {
				return;
			}
			
			int userId = apnsInfoEntity.getUser_id();

			if (userId == Integer.valueOf(ArchConstant.DEFAULT_USER_ID)) {
				ArrayList list = deviceLogic.getDeviceInfo();
				for (int i = 0; i < list.size(); i++) {
					DeviceInfoEntity entity = (DeviceInfoEntity)list.get(i);
					String deviceToken = entity.getDevice_token().trim();
					String payload = APNS.newPayload()
							.badge(1)
							.alertBody(message)
							.build();
				    service.push(deviceToken, payload);
				    System.out.println("service.push(" + deviceToken + ", " + payload + ")");
				}
			} else {
				ArrayList list = deviceLogic.getUserApnsInfo(userId);
				for (int i = 0; i < list.size(); i++) {
					UserApnsInfoEntity entity = (UserApnsInfoEntity)list.get(i);
					String deviceToken = entity.getDevice_token().trim();
					String userName = entity.getUser_name().trim();
					message = userName + " " + message;
					String payload = APNS.newPayload()
							.badge(1)
							.alertBody(message)
							.build();
				    service.push(deviceToken, payload);
				    System.out.println("service.push(" + deviceToken + ", " + payload + ")");
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
